package com.net.tkc.pro.controller;

import com.net.tkc.pro.entity.Collect;
import com.net.tkc.pro.entity.ResultEntity;
import com.net.tkc.pro.entity.SystemPush;
import com.net.tkc.pro.impl.CollectImpl;
import com.net.tkc.pro.impl.SystemPushImpl;
import com.net.tkc.pro.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/System/")
public class SystemPushController {
    ResultEntity resultEntity = null;
    @Resource
    SystemPushImpl systemPushImpl;

    @GetMapping("System")
    private String getList(
    ){
        resultEntity = new ResultEntity();
        try{
            List<SystemPush> list=systemPushImpl.getList();
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     * 管理员用户删除推送消息
     * @param id iD
     * @return
     */
    @DeleteMapping("System")
    private String delSystemPush(@RequestParam("id") int id){
        resultEntity = new ResultEntity();
        try{
            int result = systemPushImpl.delSystemPush(id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }


    @PostMapping("System")
    private String addSystemPush(
            @RequestParam("title") String title,
            @RequestParam("stext") String stext,
            @RequestParam("picture") String picture,
            @RequestParam("video") String video,
            @RequestParam("url") String url
    ){
        resultEntity = new ResultEntity();
        DateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date=new Date();
        String Time=dateFormat.format(date);
        SystemPush systemPush=new SystemPush();
        systemPush.setDt_time(Time);
        systemPush.setTitle(title);
        systemPush.setStext(stext);
        systemPush.setPicture(picture);
        systemPush.setVideo(video);
        systemPush.setUrl(url);
        try{
            int result = systemPushImpl.addSystemPush(systemPush);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
}
